<?php
/* +----------------------------------------------------------------------
// | 	xaoam.com
// |	author:am
// |	email:hi@xaoam.com
// |	基本调用
// +----------------------------------------------------------------------*/
namespace app\Index\controller;
use think\Controller;
use Casbin;
use think\Db;
use think\db\Where;

class Base extends Controller {
	protected $module;
	protected $controller;
	protected $action;
	public $reJson;
	public $basic;
    protected function initialize(){
			$this->reJson= ['code'=>0,'msg'=>'操作失败','data'=>''];
			$this->basic = $this->getBasic();
			$this->module = request()->module();
			$this->controller = request()->controller();
			$this->action = request()->action();
			$this->assign('basic',$this->basic);/*基本信息*/
			$this->assign('menu',getTree($this->getMenu(),'child'));/*菜单*/
			$this->assign('links',$this->getLinks(3));/*友链*/
			$this->assign('advert',$this->getLinks(2));/*广告*/
			$this->assign('cate',$this->getCateList());	/*分类*/ 
			$this->assign('tag',$this->getTagList());	/*标签*/ 
			$this->assign('bar_bg',$this->getBar(0));/*顶部背景*/
	}
	/*基本系统信息*/
	protected function getBasic(){
		return Db::name('basic')->find();
		
	}
	/*获取菜单*/
	protected function getMenu($type=0){
		$res = Db::name('links')->where(['status'=>1,'type'=>$type])->select();
		return $res;
	}
	protected function getTagList($id=''){
		$where          = new Where;
		$where['status'] = 1;
		$where['type']	= 1;
		if(!empty($id)){
			$where['id']    = ['in', $id];
		}
		$res = Db::name('tag')->where($where)->select();
			if($res){
			foreach($res as $key=>$row){
					$arr[$key] = $row;
					$arr[$key]['url'] = url('tag/index',['id'=>$row['id']]);
				}
			return $arr;	
			}
		return false;
	}
	protected function getCateList(){
		$res = Db::name('cate')->where(['status'=>1,'type'=>1])->select();
		foreach($res as $key=>$row){
			$arr[$key] = $row;
			if(isset($arr[$key]['url']) && empty($arr[$key]['url'])){
				$arr[$key]['url'] = url('cate/index',['id'=>$row['id']]);
			}
		}
		return getTree($arr);
	}
	protected function getBar($site=0){
		$where['status'] = 1;
		if($site === 0){
			$where['site'] = $site;
		}
			$res = Db::name('bar')->where($where)->select();
		return $res;		
	}
	protected function getLinks($site = 1){
		$res = Db::name('links')->where(['site'=>$site,'status'=>1])->select();
		return $res;
	}
	protected function getPrevNext($id,$ln=''){
		if($ln){
			$res = Db::name('article')->where(['status'=>1])->where('id',$ln,$id)->find();
			if($res){
				$res['url'] = url('article/index',['id'=>$res['id']]);
				return $res;
			}

		}
	}
}
